package com.jiepos.mpos.biz.user.controller;

import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.view.RedirectView;

import com.jiepos.mpos.biz.user.service.AppInsInfService;
import com.jiepos.mpos.common.constant.SystemConstant;
import com.jiepos.mpos.common.entity.App_Ins_InfEntity;
import com.jiepos.mpos.core.controller.BaseController;
import com.jiepos.mpos.core.entity.Pagination;
import com.jiepos.mpos.core.util.StringUtils;

/**
 * 
 * 
 * <p>标题: —— 机构信息表控制器</p>
 * <p>模块: 通用平台</p>
 * <p>版权: Copyright (c) 2017 江苏杰博实信息技术有限公司
 * @ProjectName:  [mpos] 
 * @Package com.jiepos.mpos.biz.code.controller 
 * @author 张学涛
 * @version 1.0
 * @since 2017年9月18日下午4:10:45
 * @history（历次修订内容、修订人、修订时间等）
 */
@Controller
@RequestMapping("insinf")
public class AppInsInfController extends BaseController {
	
	@Autowired
	private AppInsInfService appInsInfService;
	
	
	/**
	 * 进入机构信息列表页面
	 * @param request
	 * @param response
	 * @param pagination
	 * @return
	 */
	@RequestMapping("/getInsInfList.do")
	public ModelAndView getInsInfList(HttpServletRequest request, HttpServletResponse response,
			Pagination pagination){
		ModelAndView model = new ModelAndView("/module/user/insinf/insinf_list");		
		return super.getDetail(request, response, model);		
	}
	
	/**
	 * 查询机构信息列表信息json数据
	 * @param pagination
	 * @param appInsInfEntity
	 * @return
	 */
	@RequestMapping("/getInsInfListJson.do")
	public @ResponseBody Map getInsInfListJson(HttpServletRequest request, HttpServletResponse response,Pagination pagination,App_Ins_InfEntity appInsInfEntity,String areaname){
		return appInsInfService.getInsInfList(request,pagination, appInsInfEntity,areaname);
	}
	
	
	/**
	 * 获取机构菜单树
	 * @return
	 */
	@RequestMapping("/getInsInfTree.do")
	@ResponseBody
	public List<Map<String, Object>> getInsInfTree(HttpServletRequest request, String ID, String searchText){
		return appInsInfService.getInsInfTree(request, ID, searchText);
	}
	
	
	/**
	 * 获取用户直属机构菜单树
	 * @return
	 */
	@RequestMapping("/getDirectInsInfTree.do")
	@ResponseBody
	public List<Map<String, Object>> getDirectInsInfTree(HttpServletRequest request, String ID, String searchText){
		return appInsInfService.getDirectOrAllInsInfTree(request, ID, searchText);
	}
	
	/**
	 * 进入机构信息详细页面
	 * @param request
	 * @param response
	 * @param pagination
	 * @param ins_uuid
	 * @return
	 */
	@RequestMapping("/getInsInfDetail.do")
	public ModelAndView getInsInfDetail(HttpServletRequest request, HttpServletResponse response,
			Pagination pagination,String ins_uuid) {
		ModelAndView model = new ModelAndView("/module/user/insinf/insinf_detail");
		Map appInsInfEntity=appInsInfService.getInsInfById(request,ins_uuid);		
		model.addObject("appInsInfEntity", appInsInfEntity);
		model.addObject("areaname", appInsInfEntity.get("areaname"));
		model.addObject("subinsname", appInsInfEntity.get("subinsname"));
		return super.getDetail(request, response, model);
	}
	
	/**
	 * 保存机构
	 * @param request
	 * @param response
	 * @param appInsInfEntity
	 * @return
	 */
	@RequestMapping("/saveInsInf.do")
	public ModelAndView saveInsInf(HttpServletRequest request, HttpServletResponse response,
			App_Ins_InfEntity appInsInfEntity) {
		appInsInfService.saveInsInf(appInsInfEntity, super.getLoginUser(request, response));		
		Map map=super.getLoginUser(request, response);	
		return new ModelAndView(new RedirectView("getInsInfList.do"), map);
	}
	
	/**
	 * 删除机构信息
	 * @param request
	 * @param response
	 * @param ins_uuids
	 * @return
	 */
	@RequestMapping("/deleteInsInfByIds.do")
	public ModelAndView deleteInsInfByIds(HttpServletRequest request, HttpServletResponse response, String ins_uuids) {
		appInsInfService.deleteInsInfByIds(ins_uuids);	
		Map map=super.getLoginUser(request, response);  
		return new ModelAndView(new RedirectView("getInsInfList.do"), map);
	}
	
	/**
	 * 启用
	 * @param request
	 * @param response
	 * @param ins_uuid
	 * @return
	 */
	@RequestMapping("/enableIns.do")
	@ResponseBody
	public void enableIns(HttpServletRequest request, HttpServletResponse response, String ins_uuids){		
	    appInsInfService.updateInsSt(ins_uuids, super.getLoginUser(request, response), SystemConstant.INS_ST_1);
	}
	
	/**
	 * 停用
	 * @param request
	 * @param response
	 * @param ins_uuid
	 * @return
	 */
	@RequestMapping("/disabledIns.do")
	@ResponseBody
	public void disabledIns(HttpServletRequest request, HttpServletResponse response, String ins_uuids){		
	    appInsInfService.updateInsSt(ins_uuids, super.getLoginUser(request, response), SystemConstant.INS_ST_0);
	}
	
	/**
	 * 校验机构代码
	 * @param appInsInfEntity
	 * @return
	 */
	@RequestMapping("/checkInsInf.do")
	public @ResponseBody String checkInsInf(App_Ins_InfEntity appInsInfEntity){
		return appInsInfService.checkInsInf(appInsInfEntity);
	}
	
	/**
	 * 校验机构代码
	 * @param appInsInfEntity
	 * @return
	 */
	@RequestMapping("/checkIsChangeCurrentUserIns.do")
	@ResponseBody
	public String checkIsChangeCurrentUserIns(HttpServletRequest request, HttpServletResponse response, String ins_uuids){
		String currentUserInsUUID = String.valueOf(request.getSession().getAttribute(SystemConstant.SESSION_LOGIN_INS_UUID));
		if(StringUtils.isBlank(ins_uuids)){
			return "0";
		}else{
			if(ins_uuids.contains(StringUtils.objToString(currentUserInsUUID))){
				return "1";
			}else{
				return "0";
			}
		}
	}

}
